#------ Packages
library(car)
library(texreg)
library(ggplot2) 
library(effects)

###Call data file
ndata = read.csv("Weighted Data for Paper.csv")

prop.table(table(ndata$con2)) #8% of sample (unweighted) has felony convictions
prop.table(xtabs(ndata$wts_black~ ndata$con2)) #10% of sample (weighted) has felony convictions

length(ndata$respid) ##1110 RESPONDENTS in ndata

table(ndata$con2) ##89 respondents have personal conviction
sum(is.na(ndata$con2)) ##37 missing information on personal conviction

#-- Subset on no felony conviction. Use "wdata" for analysis
wdata = subset(ndata, ndata$con2 == 0)

length(wdata$respid) ##  984 RESPONDENTS IN wdata
table(wdata$disenfranchise)

sum(is.na(wdata$netcon2)) ##93 respondents missing information on network convictions
table(wdata$netcon2) ##106 respondents have too many convictions, removed in netcon_two
table(wdata$netcon_two)


sum(is.na(wdata$VoterID)) #7 respondents don't have Voter ID data (DC respondents)
sum(is.na(wdata$PunRate)) #7 respondents don't have PunRate data (DC respondents)
sum(is.na(wdata$polstop_freq)) #5 respondents don't have polstop_freq


#TABLE 1: Interaction of State Policy and Social Connections Predicting Participation

#Voting Models

vote0 =  lm(voter ~ disenfranchise + netcon_two + inc + edu + woman + cleanfate + ideo5 + polstop_freq + VoterID + PunRate, data = wdata, weight = wts_black)
summary(vote0)

vote1 = lm(voter ~ disenfranchise * netcon_two + inc + edu + woman + cleanfate + ideo5 + polstop_freq + VoterID + PunRate, data = wdata, weight = wts_black)
summary(vote1)

#Non-Voting Participation Models
acts0 = lm(part_scale ~ disenfranchise + netcon_two + inc + edu + woman + cleanfate + ideo5 + polstop_freq + VoterID + PunRate, data = wdata, weight = wts_black)
summary(acts0)

acts1 = lm(part_scale ~ disenfranchise * netcon_two + inc + edu + woman + cleanfate + ideo5 + polstop_freq + VoterID + PunRate, data = wdata, weight = wts_black)
summary(acts1)



#FIGURE 1: Predicted Effect of State Policy and Network Connections on Participation

#Voting figure

ef1 = effect("disenfranchise : netcon_two", vote1) #Extract data from effects package
ef1

d1 = as.data.frame(ef1) #save as data frame
d1

#Save only estimates at 1 and 0
effects.vote = subset(d1, disenfranchise == 0 | disenfranchise == 1)
effects.vote

effects.vote = subset(effects.vote, netcon_two == 0 | netcon_two == 1)
effects.vote

#Add in factor names for plotting
effects.vote$names = recode(effects.vote$netcon_two, "0 = '0 Social Ties' ; 1 = '1-2 Social Ties'")
effects.vote

effects.vote$regime = recode(effects.vote$disenfranchise, "0 = 'Enfranchised' ; 1 ='Disenfranchised'")
effects.vote

#Plot and Format
voterplot = ggplot(effects.vote, aes(x = disenfranchise, y = fit, shape = names )) +  
  geom_point(size=5.5) + geom_line( ) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.25) +
  theme_bw()  +
  ylab("Voting\n") + xlab(" ")+
  scale_x_continuous(breaks = c(0, 1), labels = c("Enfranchised\n State", "Disenfranchised\n State"))+
  scale_shape_manual(values = c(16, 17), name=" ")+
  theme(legend.position = "top", legend.text = element_text(size = 25),
        axis.text = element_text(size = 16), #Increase size of axis text
        axis.title = element_text(size = 25, face = "bold"))	
voterplot


#Non-voting participation figure

#Plot

ef2 = effect("disenfranchise : netcon_two" , acts1) #Extract data from effects package
ef2

d2 = as.data.frame(ef2) #save as data frame
d2

#Save only estimates at 1 and 0
effects.nonvoting = subset(d2, disenfranchise == 0 | disenfranchise == 1)
effects.nonvoting

effects.nonvoting = subset(effects.nonvoting, netcon_two == 0 | netcon_two == 1)
effects.nonvoting


#Add in factor names for plotting
effects.nonvoting$names = recode(effects.nonvoting$netcon_two, "0 = '0 Social Ties' ; 1 = '1-2 Social Ties'")
effects.nonvoting


effects.nonvoting$regime = recode(effects.nonvoting$disenfranchise, "0 = 'Enfranchised' ; 1 = 'Disenfranchised'")
effects.nonvoting

#Plot and Format
partplot = ggplot(effects.nonvoting, aes(x = disenfranchise, y = fit, shape = names)) +  geom_point(size=5.5) +geom_line() +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.25) +
  theme_bw()  +
  ylab("Participatory Acts\n") + xlab(" ") +
  scale_x_continuous(breaks = c(0 , 1), labels = c("Enfranchised\nState", "Disenfranchised\n State")) +
  scale_shape_manual(values = c(16, 17), name="") +
  theme(legend.position = "top", legend.text = element_text(size = 25),
        axis.text = element_text(size = 16), #Increase size of axis text
        axis.title = element_text(size = 25, face = "bold"))	
partplot		

##OUTPUT TABLE AND FIGURE

##TABLE 1
 
texreg(list(vote0, vote1, acts0, acts1),
       custom.coef.names = c( 
         "(Intercept)",
         "Disenfranchise",
         "Social Ties",
         "Income", 
         "Education", 
         "Woman",
         "Linked Fate",
         "Ideology", 
         "Stopped by Police",
         "Voter ID Law",
         "Punishment Rate",
         "Disenfranchise*Ties"
       ),
       custom.model.names = c("1: Voting Participation", "2: Voting Participation", "3: Non-Voting Participation", "4: Non-Voting Participation" ),
       dcolumn=TRUE, booktabs = TRUE, use.packages = FALSE, label ="tab:OLS", stars = c(0.05, 0.10), caption = "Interaction of State Policy and Social Connections Predicting Participation", float.pos = "hb", single.row = TRUE) 


##FIGURE 1
pdf("Figure1_Vote.pdf")
voterplot
dev.off()

pdf("Figure1_Participation.pdf")
partplot
dev.off()

